﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using NickLee.Common;
using System.Text;


namespace GPCSC.Web.QJQ
{
    public partial class QJQReport : PageBase
    {

        public double[] ad = new double[6];

        public string add = "0";


        public double[] su = new double[6];

        public string asum = "0";
        public string cout = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //txtMon.Text = DateTime.Now.Month.ToString();
                
                ReportInit("");
                Label1.Text = DateTime.Now.Month.ToString();
                nmonth.Value = DateTime.Now.Month.ToString();
            }
        }

        private void ReportInit(string type)
        {
            Hashtable htable = new Hashtable();
            DataSet ds = new DataSet();
            //读取部门列表
            if (type == "")
            {
                ds = new SqlReadXml().ReaderDataSet("sel_POST_all", htable);
            }
            else if (type == "1")
            {
                htable.Add("@tt", 0);
                ds = new SqlReadXml().ReaderDataSet("sel_POST_type", htable);
            }
            else if (type == "2")
            {
                htable.Add("@tt", 1);
                ds = new SqlReadXml().ReaderDataSet("sel_POST_type", htable);
            }
            sumss(ds, "select_CL_QJQ_byPostidAndMon", DateTime.Now.Month.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Year);



            
          //  StringBuilder sb = new StringBuilder();
          //  sb.Append("<table cellspacing='0'align='center' style='border-collapse:collapse;'><tr><td class='style1'>单位名称</td>");
          //  sb.Append("<td class='style1'>深入结对户天数</td>");
          //  sb.Append("<td class='style1'>慰问捐助折合金额（元）</td>");
          //  sb.Append("<td class='style1'>解决合理问题（个）</td>");
          //  sb.Append("<td class='style1'>办实事情况（件）</td>");
          //  sb.Append("<td class='style1'>民情日记（篇）</td></tr>");


          //  #region 第一行
          //  Hashtable htable = new Hashtable();
          //  htable.Add("@Mon", Label1.Text);
          //  DataSet ds = new SqlReadXml().ReaderDataSet("select_CL_QJQ_Sum", htable);

          //  DataView dvSum = ds.Tables[0].DefaultView;

          //  sb.Append("<tr><td class='style1'>合计</td>");
          //  sb.Append("<td class='style1'>");
          //  sb.Append(ToStringDefault(dvSum[0]["jdhs"]));
          //  sb.Append("</td>");

          //  sb.Append("<td class='style1'>");
          //  sb.Append(ToStringDefault(dvSum[0]["wwjz"]));
          //  sb.Append("</td>");

          //  sb.Append("<td class='style1'>");
          //  sb.Append(ToStringDefault(dvSum[0]["jjwt"]));
          //  sb.Append("</td>");

          //  sb.Append("<td class='style1'>");
          //  sb.Append(ToStringDefault(dvSum[0]["bssqk"]));
          //  sb.Append("</td>");

          //  sb.Append("<td class='style1'>");
          //  sb.Append(ToStringDefault(dvSum[0]["mqrj"]));
          //  sb.Append("</td>");

          //  sb.Append("</tr>"); 
          //  #endregion


          //  //BLL.CL_USER_S user = new BLL.CL_USER_S();
          //  BLL.CL_POST_S post = new BLL.CL_POST_S();

          //  string where = "1=1 order by Number ASC";
          //  //where += " and CL_ROLEname='单位管理员'";
          //  DataSet dtPost = post.GetList(where);
          //  DataView dvPost = dtPost.Tables[0].DefaultView;
          //  for (int i = 0; i < dvPost.Count; i++)
          //  {
          //      string postid = Convert.ToString(dvPost[i]["CL_POST_SOID"]);
          //      string postname = Convert.ToString(dvPost[i]["POSTNAME"]);

          //      Hashtable htSum = new Hashtable();
          //      htSum.Add("@Mon", Label1.Text);
          //      htSum.Add("@postid", postid);
          //      DataSet dsSum = new SqlReadXml().ReaderDataSet("select_CL_QJQ_byPostidAndMon", htSum);
          //      DataView dvSumbyPostid = dsSum.Tables[0].DefaultView;

          //      sb.Append("<tr>");
          //      sb.Append("<td class='style1'>");
          //      sb.Append(postname);
          //      sb.Append("</td>");

          //      sb.Append("<td class='style1'>");
          //      sb.Append(ToStringDefault(dvSumbyPostid[0]["jdhs"]));
          //      sb.Append("</td>");

          //      sb.Append("<td class='style1'>");
          //      sb.Append(ToStringDefault(dvSumbyPostid[0]["wwjz"]));
          //      sb.Append("</td>");

          //      sb.Append("<td class='style1'>");
          //      sb.Append(ToStringDefault(dvSumbyPostid[0]["jjwt"]));
          //      sb.Append("</td>");

          //      sb.Append("<td class='style1'>");
          //      sb.Append(ToStringDefault(dvSumbyPostid[0]["bssqk"]));
          //      sb.Append("</td>");

          //      sb.Append("<td class='style1'>");
          //      sb.Append(ToStringDefault(dvSumbyPostid[0]["mqrj"]));
          //      sb.Append("</td>");

          //      sb.Append("</tr>");
          //  }
          //  sb.Append("</table>");
          ////  lblQJQReport.Text = sb.ToString();

        }

        string ToStringDefault(object obj)
        {
            string returnString;
            returnString=Convert.ToString(obj);
            if (returnString == string.Empty)
            {
                returnString = "0";
            }

            return returnString;
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            dsf(DateTime.Now.Month.ToString(), DateTime.Now.Month.ToString(), "", DateTime.Now.Year);
        }
        protected void dsf(string kmonth, string jmonth, string posttype, int year)
        {
            for (int i = 0; i < 6; i++)
            {
                su[i] = 0;
            }
            // string postid = this.Text1.Value;
            string likes = this.dw.Value.Trim();
            if (likes != "")
            {
                likes = "%" + likes + "%";
                Hashtable htab = new Hashtable();
                htab.Add("@like", likes);
                DataSet ds = new DataSet();
                ds = new SqlReadXml().ReaderDataSet("sel_POST_bylike", htab);
                sumss(ds, "select_CL_QJQ_byPostidAndMon", kmonth, jmonth, year);
            }
            else
            {
                Hashtable htable = new Hashtable();
                DataSet ds = new DataSet();
                //读取部门列表
                if (posttype == "")
                {
                    ds = new SqlReadXml().ReaderDataSet("sel_POST_all", htable);
                }
                else if (posttype == "1")
                {
                    htable.Add("@tt", 0);
                    ds = new SqlReadXml().ReaderDataSet("sel_POST_type", htable);
                }
                else if (posttype == "2")
                {
                    htable.Add("@tt", 1);
                    ds = new SqlReadXml().ReaderDataSet("sel_POST_type", htable);
                }
                sumss(ds, "select_CL_QJQ_byPostidAndMon", kmonth, jmonth, year);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
            Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
            Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
            string filename = "淮安市" + DateTime.Now.Year.ToString() + "年" + "“结穷亲”活动情况表.xls";
            Response.Clear();
            Response.BufferOutput = true;
            //设定输出的字符集
            Response.Charset = "GB2312";
            //假定导出的文件名为FileName.doc
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            /**/
            ////设置导出文件的格式
            Response.ContentType = "application/ms-excel";
            //关闭ViewState


            // GrwResult.EnableViewState = false;
            LPanel.EnableViewState = false;


            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

            LPanel.RenderControl(textWriter);
            
            Response.Write(stringWriter.ToString());
            Response.End();


        }

        protected void LCallBack_Callback(object sender, NickLee.Web.UI.CallBackEventArgs e)
        {
            try
            {
                string type = e.Parameters[0].ToString();
                if (type == "0")
                {
                    //  dsf();
                    dsf(DateTime.Now.Month.ToString(), DateTime.Now.Month.ToString(), e.Parameters[1].ToString(), DateTime.Now.Year);
                }
                else if (type == "1")//季度
                {

                    int jd = Usual.f_convertInt32(e.Parameters[1].ToString());
                    int kmonth = 0;
                    int jmonth = 0;
                    switch (jd)
                    {
                        case 1:
                            kmonth = 1;
                            jmonth = 3;
                            break;
                        case 2:
                            kmonth = 4;
                            jmonth = 6;
                            break;
                        case 3:
                            kmonth = 7;
                            jmonth = 9;
                            break;
                        case 4:
                            kmonth = 10;
                            jmonth = 12;
                            break;
                        default:
                            kmonth = 1;
                            jmonth = 12;
                            break;

                    }
                    int year = Usual.f_convertInt32(e.Parameters[3].ToString());
                    dsf(kmonth.ToString(), jmonth.ToString(), e.Parameters[2].ToString(), year);

                }
                else if (type == "2")//月份
                {
                    int kmonth = Usual.f_convertInt32(e.Parameters[1].ToString());
                    int jmonth = Usual.f_convertInt32(e.Parameters[2].ToString());
                    int count = e.Parameters.Length;
                    int year = 0;
                    if (count > 4)
                    {
                        year = Usual.f_convertInt32(e.Parameters[4].ToString());
                    }
                    else
                    {
                        year = DateTime.Now.Year;
                    }
                    dsf(kmonth.ToString(), jmonth.ToString(), e.Parameters[3].ToString(), year);
                }
            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                LPanel.RenderControl(e.Output);
            }
        }

        protected void sumss(DataSet dsname, string sql, string kmonth, string jmonth, int year)
        {

            DataTable DT = new DataTable();
            DT.Columns.Add("PostName");

            DT.Columns.Add("jdhs");
            DT.Columns.Add("wwjz");
            DT.Columns.Add("jjwt");
            DT.Columns.Add("bssqk");
            DT.Columns.Add("mqrj");

            if (dsname != null)
            {
                if (dsname.Tables.Count > 0)
                {
                    if (dsname.Tables[0].Rows.Count > 0)
                    {
                        cout = dsname.Tables[0].Rows.Count.ToString();
                        //循环遍历每个部门
                        foreach (DataRow dr in dsname.Tables[0].Rows)
                        {

                            string postid = dr["CL_POST_SOID"].ToString();
                            Hashtable htables = new Hashtable();
                            htables.Add("@postid", postid);
                            htables.Add("@year", year);
                            htables.Add("@kmonth", kmonth);
                            htables.Add("@jmonth", jmonth);
                            DataSet ds = new SqlReadXml().ReaderDataSet(sql, htables);
                            //为每个部门 创建dataRow

                            DataRow drr = DT.NewRow();
                            drr["PostName"] =  dr["PostName"].ToString();

                            if (ds != null)
                            {
                                if (ds.Tables.Count > 0)
                                {
                                    if (ds.Tables[0].Rows.Count > 0)
                                    {
                                        ad[0] = Usual.f_convertDobule(ds.Tables[0].Rows[0]["jdhs"].ToString()); 
                                        ad[1] = Usual.f_convertDobule(ds.Tables[0].Rows[0]["wwjz"].ToString()); 
                                        ad[2] = Usual.f_convertDobule(ds.Tables[0].Rows[0]["jjwt"].ToString()); 
                                        ad[3] = Usual.f_convertDobule(ds.Tables[0].Rows[0]["bssqk"].ToString()); 
                                        ad[4] = Usual.f_convertDobule(ds.Tables[0].Rows[0]["mqrj"].ToString()); 
                                    }
                                }
                            }
                            drr["jdhs"] = ad[0];
                            drr["wwjz"] = ad[1];
                            drr["jjwt"] = ad[2];
                            drr["bssqk"] = ad[3];
                            drr["mqrj"] = ad[4];
                            DT.Rows.Add(drr);

                            for (int i = 0; i < 6; i++)
                            {
                                su[i] += ad[i];
                                ad[i] = 0;
                            }
                            su[5] += ad[5];
                            ad[5] = 0;
                        }
                    }
                }
            }
            Repeater1.DataSource = DT;
            Repeater1.DataBind();
        }
    }
}